package com.building.service.impl;

import com.building.dao.BuildingDao;
import com.building.entity.Building;
import com.building.entity.Dormitory;
import com.building.dao.DormitoryDao;
import com.building.response.ServerResponse;
import com.building.service.DormitoryService;
import com.building.util.DateFormatUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Dormitory)表服务实现类
 *
 * @author TianCai
 * @since 2023-01-14 15:07:18
 */
@Service("dormitoryService")
public class DormitoryServiceImpl implements DormitoryService {
    @Resource
    private DormitoryDao dormitoryDao;
    @Resource
    private BuildingDao buildingDao;
    /**
     * 通过ID查询单条数据
     *
     * @param dId 主键
     * @return 实例对象
     */
    @Override
    public Dormitory queryById(Integer dId) {
        return this.dormitoryDao.queryById(dId);
    }

    /**
     * 分页查询
     *
     * @param dormitory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Dormitory> queryByPage(Dormitory dormitory, PageRequest pageRequest) {
        long total = this.dormitoryDao.count(dormitory);
        return new PageImpl<>(this.dormitoryDao.queryAllByLimit(dormitory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param dormitory 实例对象
     * @return 实例对象
     */
    @Override
    public Dormitory insert(Dormitory dormitory) {
        this.dormitoryDao.insert(dormitory);
        return dormitory;
    }

    /**
     * 修改数据
     *
     * @param dormitory 实例对象
     * @return 实例对象
     */
    @Override
    public Dormitory update(Dormitory dormitory) {
        this.dormitoryDao.update(dormitory);
        return this.queryById(dormitory.getDId());
    }

    /**
     * 通过主键删除数据
     *
     * @param dId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer dId) {
        return this.dormitoryDao.deleteById(dId) > 0;
    }

    @Override
    public ServerResponse<Map<String, Object>> getPageInfoByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dormitory> dormitories = this.dormitoryDao.queryAllByName(name);
        PageInfo<Dormitory> dormitoryPageInfo = new PageInfo<>(dormitories);
        int size = dormitoryPageInfo.getList().size();
        if (size==0) return ServerResponse.createByErrorMessage("暂无数据!换个关键词试试吧!");
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> mapList = new ArrayList<>();
        setDormitoryInfo(mapList,dormitoryPageInfo.getList());
        map.put("list",mapList);
        map.put("total",dormitoryPageInfo.getTotal());//总数
        map.put("pageNum",dormitoryPageInfo.getPageNum());//第几页
        map.put("pageSize",dormitoryPageInfo.getPageSize());//几条数据
        return ServerResponse.createBySuccess("查询成功,共"+size+"条数据",map);
    }

    @Override
    public ServerResponse<Map<String, Object>> getPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dormitory> dormitories = this.dormitoryDao.queryAll();
        PageInfo<Dormitory> dormitoryPageInfo = new PageInfo<>(dormitories);
        Map<String,Object> map = new HashMap<>();//最大的
        List<Map<String,Object>> mapList = new ArrayList<>();//往map里边放的对象
        setDormitoryInfo(mapList,dormitoryPageInfo.getList());
        map.put("list",mapList);
        map.put("total",dormitoryPageInfo.getTotal());//总数
        map.put("pageNum",dormitoryPageInfo.getPageNum());//第几页
        map.put("pageSize",dormitoryPageInfo.getPageSize());//几条数据
        return ServerResponse.createBySuccess("获取分页数据成功!", map);
    }

    public void setDormitoryInfo(List<Map<String,Object>> mapList,List<Dormitory> dormitorys){
        for (Dormitory dormitory :dormitorys){
            Map<String,Object> map = new HashMap<>();//这样做的目的是为了给 楼宇对象新增一个字段, 这样做比较麻烦 可以用 俩边联查的方式实现但是在这里就不试了
            map.put("building",buildingDao.queryById(dormitory.getBuId()));
            map.put("id",dormitory.getDId());
            map.put("name",dormitory.getName());
            map.put("floor",dormitory.getFloor());
            map.put("liveNumber",dormitory.getLivedNumber());
            map.put("maxNumber",dormitory.getMaxNumber());
            map.put("createTime",DateFormatUtil.DataFormat(dormitory.getCreateTime()));//记得转换格式哦
            map.put("updateTime",DateFormatUtil.DataFormat(dormitory.getUpdateTime()));//记得转换格式哦
            map.put("version",dormitory.getVersion());
            map.put("status",dormitory.getStatus());
            mapList.add(map);
        }
    }

    @Override
    public ServerResponse<Dormitory> addDormitory(String name, Integer floor, Integer buid, Integer maxNumber) {
        Building building = buildingDao.queryById(buid);
        if (building==null) return ServerResponse.createByErrorMessage("添加失败!不存在此楼宇!");
        //如果现在输入的宿舍名称与数据库中已经存在的话,我们看他的对应楼宇是否相同如果先攻就不能添加
        Dormitory dormitory = this.dormitoryDao.queryByName(name);
        if (dormitory!=null && dormitory.getBuId()==buid) return ServerResponse.createByErrorMessage("此楼宇中已经存在了相同名称的宿舍,请检查输入的信息是否正确!");
        //排除了一切有问题的可能,接下来就可以大胆放心的去添加数据咯
        Dormitory dormitory1 = new Dormitory(null,buid,name,floor,0,maxNumber,new Date(),new Date(),1,true);
        int insert = dormitoryDao.insert(dormitory1);
        if (insert!=1) return ServerResponse.createByErrorMessage("添加失败,请清检查服务器");
        return  ServerResponse.createBySuccess("添加宿舍信息成功!",dormitory1);
    }

    @Override
    public ServerResponse<Dormitory> updateDormitory(Integer id, String name, Integer floor, Integer buid, Integer maxNumber) {
        Dormitory dormitory = this.dormitoryDao.queryById(id);
        if (dormitory==null) return  ServerResponse.createByErrorMessage("此数据不存在,修改失败");
        List<Dormitory> dormitories = dormitoryDao.queryAll();
        int i = dormitories.indexOf(dormitory);
        for (int count =0;count<dormitories.size();count++){
            if (i == count) continue;
            Dormitory dormitory1 = dormitories.get(count);
            if (dormitory1.getName().equals(name) && dormitory1.getBuId()==buid){
                return ServerResponse.createByErrorMessage("此楼宇中已经存在了相同名称的宿舍,请检查输入的信息是否正确!");
            }
        }
        //最大人数不能小于当前入住人数
        if (dormitory.getLivedNumber() > maxNumber) return ServerResponse.createByErrorMessage("最大人数已经小于当前入住人数了,修改失败!");
        dormitory.setFloor(floor);
        dormitory.setName(name);
        dormitory.setMaxNumber(maxNumber);
        dormitory.setVersion(dormitory.getVersion()+1);
        dormitory.setUpdateTime(new Date());
        dormitory.setBuId(buid);
        int update = dormitoryDao.update(dormitory);
        if (update!=1) return ServerResponse.createByErrorMessage("修改失败!请清检查服务器");
        return ServerResponse.createBySuccess("修改宿舍信息成功!",dormitory);
    }

    @Override
    public ServerResponse<Dormitory> deleteDormitory(Integer id) {
        Dormitory dormitory = this.dormitoryDao.queryById(id);
        if (dormitory==null) return  ServerResponse.createByErrorMessage("此数据不存在,删除失败");
        dormitory.setStatus(false);
        dormitory.setUpdateTime(new Date());
        dormitory.setVersion(dormitory.getVersion()+1);
        int update = dormitoryDao.update(dormitory);
        if (update==1) return ServerResponse.createBySuccess("删除宿舍信息成功!",dormitory);
        return ServerResponse.createByErrorMessage("删除失败!请清检查服务器");
    }

    @Override
    public ServerResponse<List<Dormitory>> getdormitoryList() {
        List<Dormitory> dormitories = this.dormitoryDao.queryByLiveNumberSmallMaxNumber();
        if (dormitories.size()==0) return ServerResponse.createByErrorMessage("当前添加的宿舍已经全部住满了人,请添加宿舍后再入住学生吧");
        return ServerResponse.createBySuccess("获取未入住宿舍信息成功!",dormitories);
    }

    @Override
    public ServerResponse<List<Dormitory>> getolddormitoryList() {
        return ServerResponse.createBySuccess("获取所有的宿舍信息",this.dormitoryDao.queryAll());
    }

    @Override
    public ServerResponse<Map<String, Object>> getCountDor() {
        List<Dormitory> dormitories = this.dormitoryDao.queryAll();
        Map<String,Object> map =  new HashMap<>();
        List<Integer> integers = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (Dormitory dormitory:dormitories){
            integers.add(dormitory.getLivedNumber());
            strings.add(dormitory.getName());
        }
        map.put("numberList",integers);
        map.put("name",strings);
        return ServerResponse.createBySuccess("获取宿舍人数和数量成功!",map);
    }
}
